package com.jspgou.cms.dao.impl;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ShopPurchaseDao;
import com.jspgou.cms.entity.ShopPurchase;

@Repository
public class ShopPurchaseDaoImpl extends HibernateBaseDao<ShopPurchase, Long> implements ShopPurchaseDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}

	public ShopPurchase findById(Long id) {
		ShopPurchase entity = get(id);
		return entity;
	}

	public ShopPurchase save(ShopPurchase bean) {
		getSession().save(bean);
		return bean;
	}

	public ShopPurchase deleteById(Long id) {
		ShopPurchase entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<ShopPurchase> getEntityClass() {
		return ShopPurchase.class;
	}
	
	private Finder getFinderForTag(String merchantid) {
		String hql = "from ShopPurchase where merchantid='"+merchantid+"'";
		Finder f = Finder.create(hql);
		f.append(" order by id desc");
		f.setCacheable(true);
		return f;
	}

	public Pagination getPageByTag(String merchantid,int pageNo,int pageSize) {
		
		Finder f = getFinderForTag(merchantid);
		return find(f, pageNo, pageSize);
	}
}